package com.qs.cwams.controller;


import com.qs.common.framework.dto.QsResult;
import com.qs.common.framework.dto.Query;
import com.qs.cwams.pojo.view.*;
import com.qs.cwams.service.AtteConfigService;
import com.qs.cwams.service.AtteRecordService;
import com.qs.cwams.service.AtteRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author suncheng
 */
@RestController
@RequestMapping("/atte")
public class AtteController {

    @Autowired
    private AtteRecordService atteRecordService;
    @Autowired
    private AtteRuleService atteRuleService;
    @Autowired
    private AtteConfigService atteConfigService;

    @PostMapping("/query/statistic")
    public QsResult queryStatistic(@RequestBody AtteStatisticVO atteStatisticVO){
        QsResult qsResult = atteRecordService.queryStatistic(atteStatisticVO);
        return qsResult;
    }

    @PostMapping("/query/total")
    public QsResult queryTotal(@RequestBody AtteDateVO atteDateVO){
        if (atteDateVO.getYear() == null || atteDateVO.getMonth() == null) {
            return QsResult.fail(400, "参数错误");
        } else {
            Double res = atteRecordService.queryTotal(atteDateVO);
            return QsResult.ok().setResult(res);
        }
    }

    @PostMapping("/calc")
    public QsResult calc(@RequestBody AtteCalcVO atteCalcVO){
        List<AtteCalcVO> list = atteRecordService.calc(atteCalcVO);
        return QsResult.ok().setResult(list);
    }

    @PostMapping("/record")
    public QsResult record(@RequestBody AtteRecordVO atteRecordVO){
        return atteRecordService.record(atteRecordVO);
    }

    @PostMapping("/result")
    public QsResult result(@RequestBody AtteDateVO atteDateVO){
        atteRecordService.result(atteDateVO);
        return QsResult.ok();
    }

    @PostMapping("/export")
    public void export(@RequestBody AtteDateVO atteDateVO, HttpServletResponse response){
        atteRecordService.export(atteDateVO, response);
    }

    @PostMapping("/set/rule")
    public QsResult setRule(@RequestBody List<AtteRuleVO> rules){
        return atteRuleService.setRule(rules);
    }

    @PostMapping("/del/rule")
    public QsResult delRule(@RequestBody List<Long> ruleIds){
        return atteRuleService.delRule(ruleIds);
    }

    @PostMapping("/query/rule")
    public QsResult queryRule(@RequestBody Query<AtteRuleVO> rulePage){
        return atteRuleService.queryRule(rulePage.getParam(), rulePage.getPage());
    }

    @PostMapping("/set/add")
    public QsResult setAdd(){
        return atteRuleService.setAdd();
    }

    @PostMapping("/query/config")
    public QsResult queryConfig(){
        return atteConfigService.queryConfig();
    }

    @PostMapping("/set/config")
    public QsResult setConfig(@RequestBody AtteConfigVO atteConfigVO){
        return atteConfigService.setConfig(atteConfigVO);
    }

    @PostMapping("/query/user")
    public QsResult queryUser(){
        return atteConfigService.queryUser();
    }

    @PostMapping("/set/user")
    public QsResult setUser(@RequestBody List<AtteUserVO> users){
        return atteConfigService.setUser(users);
    }
}
